define(["backbone", "_template","framework", "_global", "jquery", "_cartData", "_clearCart", "_base"],
    function (Backbone, template,framework, _gl, $, _CD, _clearCart, _base) {
        return Backbone.View.extend({
            el: '#pageSwitch',
            events: {
                "click #chooseAll": "chooseAll",
                "click .chooseOne": "chooseOne",
                "click #del": "del",
                "click #sureOrder": "sureOrder",
                "click .reduce": "reduce",
                "click .add": "add"
            },
            initialize: function () {
            },
            render: function (navCode) {
                var self = this;
                self.loadIndex = framework.load();
                framework.initLeftNav(navCode, function () {
                    $(self.el).html('');
                    var data = {
                        totalPrice: _CD.getTotalPrice(),
                        data: _CD.getData().products
                    };
                    var tHtml = template("templateCart", data);
                    $(self.el).html(tHtml);
                    _gl.closeAll();
                    _gl.backTop();
                    self.hideDetail();
                    framework.restartAD(navCode);
                });
            },
            //操作 全选元素
            chooseAll: function (e) {
                var $this = $(e.target);
                var scope = this;
                var isCheck = $this.attr("data-checked");
                if (isCheck == 'true') {
                    scope.getAllCheckbox().each(function (i, checkbox) {
                        $(checkbox).prop("checked", false);
                        var pno = $(checkbox).parents(".shoppingCart-item").eq(0).attr("data-pno");
                        _CD.setProductBuy(pno, $(checkbox).is(":checked"));
                    });
                    $("#cartTotalPrice").html("0.00");
                    $this.attr("data-checked", "false");
                } else {
                    scope.getAllCheckbox().each(function (i, checkbox) {
                        $(checkbox).prop("checked", true);
                        var pno = $(checkbox).parents(".shoppingCart-item").eq(0).attr("data-pno");
                        _CD.setProductBuy(pno, $(checkbox).is(":checked"));
                    });
                    scope.setTotalPrice();
                    $this.attr("data-checked", "true");
                }
            },
            //删除选中的商品
            del: function (e) {
                var $this = $(e.target);
                var scope = this;
                var allChooseArr = scope.getCheckedCheckbox()
                if (!!allChooseArr && allChooseArr.length > 0) {
                    var layerIndex = _gl.confirm({
                        title: '温馨提示',
                        content: '确认移除选中的商品么？',
                        btnName: ['取消', '确定'],
                        yes: function () {
                            allChooseArr.each(function (i, checkbox) {
                                var $product = $(checkbox).parents(".shoppingCart-item").eq(0);
                                _CD.del($($product).attr("data-pno"));
                                $product.remove();
                            });
                            scope.setTotalPrice();
                            scope.setCartNumb();
                            _gl.close(layerIndex);
                        },
                        no: function () {
                            _gl.close(layerIndex);
                            return false;
                        }
                    });
                }
            },
            //确认订单
            sureOrder: function (e) {
                var $this = $(e.target);
                var scope = this;
                var $cartTotalPrice = $("#cartTotalPrice");
                if (parseFloat($cartTotalPrice.html()) <= 0) {
                    return false;
                }
            },
            //操作指定商品数量减1
            reduce: function (e) {
                var $this = $(e.target);
                var scope = this;
                if (_CD.isOverdue()) {
                    _clearCart.clearCart({
                        clearCartFunc: function () {
                            _CD.clear();
                            scope.render();
                        },
                        unClearCartFunc: function () {

                        }
                    });
                }

                var $product = $this.parents(".shoppingCart-item").eq(0);
                var $showNumb = $product.find(".numb");
                var $unitProductTotalPrice = $product.find(".unitProductTotalPrice");
                var $checkbox = $product.find("input:checkbox[name=prodCheckbox]");
                var price = $product.find(".priceShow").html();
                var pno = $product.attr("data-pno");
                var gNumb = 0;
                if ($showNumb.val() == 1) {
                    var layerIndex = _gl.confirm({
                        title: '温馨提示',
                        content: '确认移除该商品么？',
                        btnName: ['取消', '确定'],
                        yes: function () {
                            _CD.del(pno);
                            $product.remove();
                            scope.checkChooseAll();
                            scope.setTotalPrice();
                            scope.setCartNumb();
                            _gl.close(layerIndex);
                        },
                        no: function () {
                            _gl.close(layerIndex);
                            return false;
                        }
                    });
                   
                } else {
                    _CD.reduce(pno);
                    gNumb = _CD.getNumb(pno);
                    $showNumb.val(gNumb);
                    $unitProductTotalPrice.html(_base.accMul(parseFloat(gNumb), parseFloat(price)));
                    scope.checkChooseAll();
                    scope.setTotalPrice();
                    scope.setCartNumb();
                }
                /*if ($showNumb.val() <= 2) {
                    $this.prop("disabled", true);
                    if ($showNumb.val() == 2) {
                        _CD.reduce(pno);
                        gNumb = _CD.getNumb(pno);
                        $showNumb.val(gNumb);
                        $unitProductTotalPrice.html(_base.accMul(parseFloat(gNumb), parseFloat(price)));
                        if (!$checkbox.is(":checked")) {
                            $checkbox.prop("checked", true);
                            scope.checkChooseAll();
                        }
                    } else {

                    }
                } else {
                    _CD.reduce(pno);
                    gNumb = _CD.getNumb(pno);
                    $showNumb.val(gNumb);
                    $unitProductTotalPrice.html(_base.accMul(parseFloat(gNumb), parseFloat(price)));
                    if (!$checkbox.is(":checked")) {
                        $checkbox.prop("checked", true);
                        scope.checkChooseAll();
                    }
                }
                scope.checkChooseAll();
                scope.setTotalPrice();
                scope.setCartNumb();
                */
            },
            //操作指定商品数量加1
            add: function (e) {
                var $this = $(e.target);
                var scope = this;
                if (_CD.isOverdue()) {
                    _clearCart.clearCart({
                        clearCartFunc: function () {
                            _CD.clear();
                            scope.render();
                        },
                        unClearCartFunc: function () {

                        }
                    });
                }

                var $product = $this.parents(".shoppingCart-item").eq(0);
                var $showNumb = $product.find(".numb");
                var $unitProductTotalPrice = $product.find(".unitProductTotalPrice");
                var $checkbox = $product.find("input:checkbox[name=prodCheckbox]");
                var $reduce = $product.find("button.reduce");
                var price = $product.find(".priceShow").html();
                var pno = $product.attr("data-pno");
                var gNumb = 0;
                if ($reduce.prop("disabled")) {
                    $reduce.prop("disabled", false);
                }
                _CD.save({ pno: pno, price: price });
                gNumb = _CD.getNumb(pno);
                $showNumb.val(gNumb);
                $unitProductTotalPrice.html(_base.accMul(parseFloat(gNumb), parseFloat(price)));
                if (!$checkbox.is(":checked")) {
                    $checkbox.prop("checked", true);
                    var pno = $checkbox.parents(".shoppingCart-item").eq(0).attr("data-pno");
                    _CD.setProductBuy(pno, $checkbox.is(":checked"));
                    scope.checkChooseAll();
                }
                scope.setTotalPrice();
                scope.setCartNumb();
            },
            //选中某一个商品
            chooseOne: function (e) {
                var $this = $(e.target);
                var scope = this;
                var pno = $this.parents(".shoppingCart-item").eq(0).attr("data-pno");
                _CD.setProductBuy(pno, $this.is(":checked"));
                scope.checkChooseAll();
                scope.setTotalPrice();
            },
            //选中全选元素
            checkChooseAll: function () {
                var $chooseAll = $("#chooseAll");
                var allCheckedArr = this.getCheckedCheckbox();
                if (this.getAllCheckbox().length > 0) {
                    if (this.getAllCheckbox().length == allCheckedArr.length) {
                        $chooseAll.prop("checked", true);
                        $chooseAll.attr("data-checked", "true");
                    } else {
                        $chooseAll.prop("checked", false);
                        $chooseAll.attr("data-checked", "false");
                    }
                }
            },
            //获取所有选中的物品
            getCheckedCheckbox: function () {
                return $("#cartGoodsList").find("input:checkbox[name=prodCheckbox]:checked");
            },
            //获取所有商品
            getAllCheckbox: function () {
                return $("#cartGoodsList").find("input:checkbox[name=prodCheckbox]");
            },
            // 设置选中的物品总金额
            setTotalPrice: function () {
                var $totalPrice = $("#cartTotalPrice");
                var allChooseArr = this.getCheckedCheckbox();
                var price = 0.00;
                allChooseArr.each(function (i, item) {
                    price = _base.accAdd(parseFloat(price), parseFloat($(item).parents(".shoppingCart-item").eq(0).find(".unitProductTotalPrice").html()));
                });
                if (price <= 0) {
                    price = '0.00';
                }
                $totalPrice.html(price);
            },
            // 获取导航菜单nav中cart元素
            getNavCartEl: function () {
                return $("#cartNumb");
            },
            //设置购物车中物品数量
            setCartNumb: function () {
                if (parseInt(_CD.getProductsTotal()) < 0) {
                    this.getNavCartEl().html(parseInt(_CD.getProductsTotal())).hide();
                } else {
                    this.getNavCartEl().html(parseInt(_CD.getProductsTotal())).show();
                }
            },
            hideDetail: function () {
                $("#floor1").show();
                $("#floor2").hide();
            }
        });
    });